package com.example.teststatisticesinterface.activity;

import com.example.teststatisticesinterface.R;
import com.example.teststatisticesinterface.R.id;
import com.example.teststatisticesinterface.db.Dbhelper;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DbTableListActivity extends Activity {
	private ListView lv;
	private TextView tvId, tvType, tvStrategy, tvEventTime, tvApp_log_body, tvCategoryId, tvChargeType, tvClickIndex, tvVisitTime, tvRecord_time;
	private Spinner mSpinner;

	private static final int SPINNER_ACT_LOCATION = 0;
	private static final int SPINNER_ACTIONS = 1;
	private static final int SPINNER_APP_START = 2;
	private static final int SPINNER_DOWNLOAD = 3;
	private static final int SPINNER_SPECIAL_CLICK = 4;
	private static final int SPINNER_SHARE = 5;
	private static final int SPINNER_TABLE_APP_LOG = 6;
	private static final int SPINNER_TABLE_PAGE_VISIT = 7;

	private Cursor cursor;

	private String[] act_locationFrom = new String[] { "_id", "act_location_id", "act_location_position", "act_location_amount", "act_location_other", "act_location_date", "record_time" };
	private String[] actionsFrom = new String[] { "_id", "action_id", "action_amount", "action_date", "record_time" };
	private String[] appstartFrom = new String[] { "_id", "ssoid", "time", "record_time" };
	private String[] downloadFrom = new String[] { "_id", "productId", "productDesc", "sourceCode", "sourceVersion", "categoryId", "chargeType", "clickIndex", "visitTime", "record_time" };
	private String[] special_clickFrom = new String[] { "_id", "cid", "desc", "type", "time", "record_time" };
	private String[] shareFrom = new String[] {};
	private String[] applogFrom = new String[] { "_id", "type", "strategy", "eventTime", "app_log_body" };
	private String[] pagevisitFrom = new String[] { "_id", "type", "strategy", "eventTime", "page_visit_body" };

	// 5
	private int[] app_logTo = { R.id.tv_id, R.id.tv_type, R.id.tv_strategy, R.id.tv_eventTime, R.id.tv_app_log_body };
	// 4
	private int[] appstartTo = { R.id.tv_id, R.id.tv_type, R.id.tv_strategy, R.id.tv_eventTime };
	//6
	private int[] shareTo={R.id.tv_id, R.id.tv_type, R.id.tv_strategy, R.id.tv_eventTime, R.id.tv_app_log_body, R.id.tv_categoryId};
	//7
	private int[] act_locationTo={R.id.tv_id, R.id.tv_type, R.id.tv_strategy, R.id.tv_eventTime, R.id.tv_app_log_body, R.id.tv_categoryId, R.id.tv_chargeType};
	// 10
	private int[] downloadTo = { R.id.tv_id, R.id.tv_type, R.id.tv_strategy, R.id.tv_eventTime, R.id.tv_app_log_body, R.id.tv_categoryId, R.id.tv_chargeType, R.id.tv_clickIndex, R.id.tv_visitTime,
			R.id.tv_record_time };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dbtablelist_activity);
		initView();
	}

	private void initView() {
		mSpinner = (Spinner) findViewById(R.id.spinner1);
		lv = (ListView) findViewById(R.id.lv_cursor);

		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(DbTableListActivity.this, R.array.tablename, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinner.setAdapter(adapter);
		mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				switch (arg2) {
				
				case SPINNER_ACT_LOCATION:
					
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_ACT_LOCATION);
					if (cursor != null) {
						lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, act_locationFrom, act_locationTo));
						cursor = null;
					} else {
						lv.setVisibility(View.INVISIBLE);
					}
					break;
					
				case SPINNER_ACTIONS:
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_ACTIONS);
					if (cursor != null) {
						lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, actionsFrom, appstartTo));
						cursor = null;
					} else {
						lv.setVisibility(View.INVISIBLE);
					}
					break;
					
				case SPINNER_APP_START:
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_APP_START);
					lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, appstartFrom, appstartTo));
					cursor = null;
					break;
				case SPINNER_DOWNLOAD:
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_DOWNLOAD);
					if (cursor != null) {
						lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, downloadFrom, downloadTo));
						cursor = null;
					} else {
						lv.setVisibility(View.INVISIBLE);
					}
					break;
				case SPINNER_SPECIAL_CLICK:
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_SPECIAL_CLICK);
					if (cursor != null) {
						lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, special_clickFrom, shareTo));
						cursor = null;
					} else {
						lv.setVisibility(View.INVISIBLE);
					}
					break;
				case SPINNER_SHARE:
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_SHARE);
					if (cursor != null) {
						lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, shareFrom, shareTo));
						cursor = null;
					} else {
						lv.setVisibility(View.INVISIBLE);
					}
					break;
				case SPINNER_TABLE_APP_LOG:
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_APP_LOG);
					if (cursor!=null) {
						lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, applogFrom, app_logTo));
						cursor = null;
					}else {
						lv.setVisibility(View.INVISIBLE);
					}
					break;
				case SPINNER_TABLE_PAGE_VISIT:
					cursor = Dbhelper.loadAllFromTable(DbTableListActivity.this, Dbhelper.TABLE_TABLE_PAGE_VISIT);
					if (cursor!=null) {
						lv.setAdapter(new MyCursorAdapter(DbTableListActivity.this, R.layout.dbtablelist_item, cursor, pagevisitFrom, app_logTo));
					}else {
						lv.setVisibility(View.INVISIBLE);
					}
					break;
				default:
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				showToast("onNothingSelected");
			}
		});

		// int id=cursor.getInt(cursor.getColumnIndex("_id"));
		// String type=cursor.getString(cursor.getColumnIndex("type"));
		// String strategy=cursor.getString(cursor.getColumnIndex("strategy"));
		// int eventTime=cursor.getInt(cursor.getColumnIndex("eventTime"));
		// String
		// app_log_body=cursor.getString(cursor.getColumnIndex("app_log_body"));

	}

	class myadapter extends SimpleCursorAdapter {

		public myadapter(Context context, int layout, Cursor c, String[] from, int[] to) {
			super(context, layout, c, from, to);
		}

	}

	class MyCursorAdapter extends SimpleCursorAdapter {

		public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
			super(context, layout, c, from, to);
		}

		// Context context = null;
		// int viewResId;
		@Override
		public void bindView(View view, Context arg1, Cursor arg2) {
			// tvId=(TextView) findViewById(R.id.tv_id);
			// tvType=(TextView) findViewById(R.id.tv_type);
			// tvStrategy=(TextView) findViewById(R.id.tv_strategy);
			// tvEventTime=(TextView) findViewById(R.id.tv_eventTime);
			// tvApp_log_body=(TextView) findViewById(R.id.tv_app_log_body);
			//
			// if (cursor!=null) {
			// tvId.setText(cursor.getInt(cursor.getColumnIndex("_id")));
			// tvType.setText(cursor.getString(cursor.getColumnIndex("type")));
			// tvType.setText(cursor.getString(cursor.getColumnIndex("strategy")));
			// tvEventTime.setText(cursor.getInt(cursor.getColumnIndex("eventTime")));
			// tvApp_log_body.setText(cursor.getString(cursor.getColumnIndex("app_log_body")));
			// }else {
			//
			// }
			super.bindView(view, arg1, arg2);

		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			// listview 每次得到一个item 都要用view去绘制 ，通过getView方法得到view
			// converView 只能 绘制一个屏幕
			convertView = null;
			if (convertView == null) {
				view = super.getView(position, convertView, parent);
			} else {
				view = convertView;
			}
			//
			int[] colors = { Color.WHITE, Color.rgb(219, 238, 244) };
			view.setBackgroundColor(colors[position % 2]);
			return view;
		}

		// @Override
		// public View newView(Context arg0, Cursor arg1, ViewGroup parent) {
		// super.newView(arg0, arg1, parent);
		// // TextView view = null;
		// // LayoutInflater vi = null;
		// // vi = (LayoutInflater)
		// context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// // view = (TextView) vi.inflate(viewResId, parent, false);
		// // // v =(TextView)vi.inflate(textViewResourceId,null);
		// // Log.i("hubin", "newView" + view);
		// // return view;
		// }

	}
	public void showToast(String str) {
		Toast.makeText(DbTableListActivity.this, str, Toast.LENGTH_SHORT).show();
	}
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		try {
			super.onConfigurationChanged(newConfig);
			if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
				Log.v("DbTableListActivity", "DbTableListActivity" + "onConfigurationChanged_ORIENTATION_LANDSCAPE");
			} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				Log.v("DbTableListActivity", "DbTableListActivity" + "onConfigurationChanged_ORIENTATION_PORTRAIT");
			}
		} catch (Exception ex) {
		}
	}
}
